import 'package:flutter/material.dart';
import 'package:proj_shop/constant/constant.dart';

typedef void ITextFieldCallBack(String content);

class TextFieldPwd extends StatefulWidget {
  final ITextFieldCallBack contentStrCallBack;

  const TextFieldPwd({Key key, this.contentStrCallBack}) : super(key: key);
  @override
  _TextFieldPwdState createState() => _TextFieldPwdState();
}

String _inputAccount = "";
bool _isShowDelete = false;

class _TextFieldPwdState extends State<TextFieldPwd> {
  TextEditingController _controller = new TextEditingController.fromValue(
    TextEditingValue(
      text: _inputAccount,
      selection: new TextSelection.fromPosition(
        TextPosition(
            affinity: TextAffinity.downstream, offset: _inputAccount.length),
      ),
    ),
  );
  @override
  Widget build(BuildContext context) {
    return Container(
      child: TextField(
        controller: _controller,
        style: TextStyle(color: Color(0xff333333), fontSize: 14),
        keyboardType: TextInputType.text,
        maxLength: 20,
        maxLines: 1,
        obscureText: true,
        decoration: buildInputDecoration(),
        onChanged: (str) {
          setState(() {
            _inputAccount = str;
            _isShowDelete = (_inputAccount.isNotEmpty);
            widget.contentStrCallBack(_inputAccount);
          });
        },
      ),
    );
  }

  InputDecoration buildInputDecoration() => InputDecoration(
        counterText: "",
        hintText: "请输入密码",
        contentPadding: EdgeInsets.only(left: 0, top: 14, bottom: 14),
        hintStyle: TextStyle(color: Color(0xff8C8C8C), fontSize: 14),
        focusedBorder: UnderlineInputBorder(
          borderSide: BorderSide(color: Colors.orange),
        ),
        enabledBorder: UnderlineInputBorder(
          borderSide: BorderSide(color: Color(0xffDADADA)),
        ),
        fillColor: Colors.transparent,
        filled: true,
        suffixIcon: buildSuffixIcon(),
      );

  Widget buildSuffixIcon() {
    return _isShowDelete
        ? new Container(
            width: 14.0,
            height: 14.0,
            child: new IconButton(
              padding: const EdgeInsets.all(0.0),
              iconSize: 14.0,
              icon: Image.asset(
                Constant.ASSETS_IMG + 'icon_et_delete.png',
                width: 14.0,
                height: 14.0,
              ),
              onPressed: () {
                setState(
                  () {
                    _inputAccount = "";
                    _isShowDelete = (_inputAccount.isNotEmpty);
                    widget.contentStrCallBack(_inputAccount);
                  },
                );
              },
            ),
          )
        : Text('');
  }
}
